package com.zzc.zzcapi.service.impl.inner;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zzc.keleapiCommon.service.InnerUserInterfaceInfoService;
import com.zzc.zzcapi.common.ErrorCode;
import com.zzc.zzcapi.exception.BusinessException;
import com.zzc.zzcapi.mapper.UserInterfaceInfoMapper;
import com.zzc.zzcapi.model.entity.UserInterfaceInfo;
import com.zzc.zzcapi.service.UserInterfaceInfoService;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.Resource;

@DubboService
public class InnerUserInterfaceInfoServiceImpl implements InnerUserInterfaceInfoService {
    @Resource
    UserInterfaceInfoService userInterfaceInfoService;
    @Resource
    UserInterfaceInfoMapper userInterfaceInfoMapper;
    @Override
    public void invokeCount(Long interfaceInfoId, Long userId) {
        QueryWrapper<UserInterfaceInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("interfaceInfoId", interfaceInfoId).eq("userId", userId);
        UserInterfaceInfo interfaceInfo = userInterfaceInfoService.getOne(queryWrapper);
        if(interfaceInfo == null || interfaceInfo.getLeftNum() - 1 < 0) {
            throw new BusinessException(ErrorCode.EXHAUSTED_ERROR);
        }
        userInterfaceInfoMapper.invokeCount(interfaceInfoId, userId);
    }
}
